# import os
# import datetime
#
#
# def log(msg):
#     print(msg)
#     with open(f'{settings.ROOT_PATH}/environment/log.log', 'a') as file:
#         file.write(f'{datetime.datetime.now()}: {msg}\n')
#
#
# try:
#     import sys
#     import time
#     import signal
#     import subprocess
#     import requests
#     from common.third_party.devmem import DevMem
#     from settings import settings
#
#     DevMem(address=0x43CC_0000).write(0, 10_000)
#     DevMem(address=0x43CC_0004).write(0, 1)
#
#     kills = [
#         'sudo pkill -SIGKILL -f "/home/debian/rcs/software/radius_control_software_dma_server"',
#         'sudo pkill -SIGKILL -f "/home/debian/rcs/software/radius_control_backend/services/server_integration/run.py"',
#         'sudo pkill -SIGKILL -f "/home/debian/rcs/software/radius_control_backend/services/ocb/run.py"',
#         'sudo pkill -SIGKILL -f "/home/debian/rcs/software/radius_control_backend/services/relays_module/run.py"',
#         'sudo pkill -SIGKILL -f "/home/debian/rcs/software/radius_control_backend/services/telemetry/run.py"',
#         'sudo pkill -SIGKILL -f "/home/debian/rcs/software/radius_control_backend/services/automatic_control/run.py"',
#         'sudo pkill -SIGKILL -f "/home/debian/rcs/software/radius_control_backend/services/services_module/run.py"',
#         'sudo pkill -SIGKILL -f "/home/debian/rcs/software/radius_control_backend/services/ad936x/run.py"',
#         'sudo pkill -SIGKILL -f "/home/debian/rcs/software/radius_control_backend/services/attenuators/run.py"',
#         'sudo pkill -SIGKILL -f "/home/debian/rcs/software/radius_control_backend/services/control/run.py"',
#         'sudo pkill -SIGKILL -f "/home/debian/rcs/software/radius_control_backend/services/dsp/run.py"',
#         'sudo pkill -SIGKILL -f "/home/debian/rcs/software/radius_control_frontend/main.py"',
#         'sudo pkill -SIGKILL -f "/home/debian/rcs/software/radius_control_backend/services/killer/run.py"',
#         'sudo pkill -SIGKILL -f "/home/debian/rcs/software/radius_control_backend/main.py"',
#     ]
#     time.sleep(10)
#     while True:
#         time.sleep(3)
#         try:
#             from services.server_integration.api import ServerIntegrationApi, Alarm
#             SI_API = ServerIntegrationApi()
#             unit_configuration = SI_API.get_full_configuration().unit_configuration
#             serial_number = unit_configuration.main_module.serial_number
#             device_name = unit_configuration.main_module.device_name
#             server_address = f'{unit_configuration.server.ip}:{unit_configuration.server.port}'
#         except Exception as e:
#             pass
#         else:
#             break
#
#     artix_mem = DevMem(address=0x4120_000033566)
#     power_mem = DevMem(address=0x43C0_0000)
#     unknown_mem = DevMem(address=0x4122_0000)
#
#     def await_interrupt():
#         log('await_interrupt')
#         mem = DevMem(address=0x4121_0000)
#         while mem.read(0).get_value() == 0:
#             time.sleep(0.01)
#
#         # log('await_interrupt')
#         # fd = os.open('/dev/uio1', os.O_RDWR)
#         # info = (1).to_bytes(4, byteorder=sys.byteorder)
#         # while True:
#         #     os.write(fd, info)
#         #     nb = os.read(fd, 4)
#         #     if nb == info:
#         #         break
#
#
#     def log_alarm_local():
#         log('local_log_alarm')
#         SI_API.raise_alarm('PowerFailure', value=True)
#
#
#     def slaughter():
#         os.system('sudo pkill -SIGKILL -f "/home/debian/rcs/software/radius_control_software_dma_server"')
#         os.system('pkill python3.10')
#         time.sleep(0.3)
#         for _ in range(2):
#             for cmd in kills:
#                 os.system(cmd)
#
#         # os.system('sudo pkill -SIGKILL -f "/home/debian/rcs/software/radius_control_software_dma_server"')
#
#         # pid_list = list(map(int, subprocess.Popen(['pgrep', 'python'], stdout=subprocess.PIPE).stdout.readlines()))
#         # pid_list.remove(os.getpid())
#         #
#         # for pid in pid_list:
#         #     os.kill(pid, signal.SIGKILL)
#
#
#     def off_kintex():
#         log('off_kintex')
#         # unknown_mem.write(0, 0)
#         artix_mem.write(0, 0)
#         power_mem.write(0, 0)
#
#
#     def set_clocks():
#         log('set_clocks')
#         os.system('sudo echo 0 > /sys/class/fclkcfg/fpga-clk0/enable')
#         os.system('sudo echo 0 > /sys/class/fclkcfg/fpga-clk1/enable')
#         os.system('echo 333334 > /sys/devices/system/cpu/cpufreq/policy0/scaling_setspeed')
#
#
#     def log_alarm_to_server():
#         for i in range(30):
#             log(f'log_alarm_to_server {i}')
#             try:
#                 alarm = {
#                     "code": 108,
#                     "value": 1,
#                     "state": True,
#                     "time": datetime.datetime.now(),
#                     "serial_number": 'tt'
#                 }
#                 requests.post('https://9ce7b59e-806b-4c0b-9e3b-cbaaa041e9fc.mock.pstmn.io', data=alarm)
#                 # requests.post(f"http://{server_address}/api/alarms/set_alarm/", data=alarm)
#             except Exception as e:
#                 log(f'errot: {e}')
#                 pass
#     await_interrupt()
#     log_alarm_local()
#     slaughter()
#     off_kintex()
#     set_clocks()
#     # log_alarm_to_server()
# except Exception as e:
#     log(f'error: {e}')
#     pass
#
# log('reboot')
# os.system('sudo reboot')
#
#


